﻿using System.Collections.Generic;

namespace Driven.ProcessScheduler
{
    public interface IScheduleManager
    {
        /// <summary>
        /// A list of all the schedules that have been configured
        /// </summary>
        List<ISchedule> Schedules { get; }

        /// <summary>
        /// Asks all Schedules to determine if they are due and execute themselves
        /// </summary>
        /// <returns>A list of messages (error or information) to write to the log</returns>
        List<string> Execute();
    }

    public class ScheduleManager : IScheduleManager
    {
        public List<ISchedule> Schedules { get; private set; }

        public ScheduleManager(List<ISchedule> schedules)
        {
            Schedules = schedules;
        }

        public List<string> Execute()
        {
            var log = new List<string>();
            
            //log.Add("System: Log Started");
            
            foreach (Schedule s in this.Schedules)
            {
                string[] messages = s.Execute();
                for (int i = 0; i < messages.Length; i++) messages[i] = s.Component.ToString() + " - " + messages[i];
                log.AddRange(messages);
            }
            //log.Add("System: Log Ended");

            return log;
        }

    }
}